//
//  RootTabBarController.m
//  FPTest
//
//  Created by zhaohong on 2017/11/8.
//  Copyright © 2017年 zhaohong. All rights reserved.
//

#import "RootTabBarController.h"
#import "HomeViewController.h"
#import "MessageViewController.h"
#import "FriendViewController.h"
#import "OurViewController.h"
#import "TabBarBtn.h"

@interface RootTabBarController ()
{
    NSUInteger selectIndex;
    UIView *tabbarView;
}

@end

@implementation RootTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //初始化控制器
    [self initViews];
    
    //初始化标签栏
    [self initTabbarView];
}

//初始化控制器
- (void)initViews
{
    HomeViewController *homeCon = gotStoryController(@"HomeViewController");
    BaseNaviController *homeNavi = [[BaseNaviController alloc]initWithRootViewController:homeCon];
    
    MessageViewController *messageCon = gotStoryController(@"MessageViewController");
    BaseNaviController *messageNavi = [[BaseNaviController alloc]initWithRootViewController:messageCon];
    
    FriendViewController *friendCon = gotStoryController(@"FriendViewController");
    BaseNaviController *friendNavi = [[BaseNaviController alloc]initWithRootViewController:friendCon];
    
    OurViewController *ourCon = gotStoryController(@"OurViewController");
    BaseNaviController *ourNavi = [[BaseNaviController alloc]initWithRootViewController:ourCon];
    
    NSArray *navis = @[homeNavi,messageNavi,friendNavi,ourNavi];
    self.viewControllers = navis;
    
}


/**
 初始化标签栏
 
 - returns: void
 */
#pragma mark 初始化标签栏
- (void)initTabbarView
{
    tabbarView = [[UIView alloc]initWithFrame:self.tabBar.bounds];
    tabbarView.backgroundColor = [UIColor whiteColor];
    [self.tabBar addSubview:tabbarView];
    self.tabBar.autoresizesSubviews = NO;
    tabbarView.autoresizesSubviews = NO;
    
    
    NSArray *backgroud = @[@"tb_home_n",@"tb_message_n",@"tb_bbs_n",@"tb_me_n"];
    NSArray *heightBackground = @[@"tb_home_s",@"tb_message_s",@"tb_bbs_s",@"tb_me_s"];
    NSArray *titles = @[@"首页",@"消息",@"好友",@"我的"];
    for (int i=0; i<backgroud.count; i++) {
        NSString *backImage = backgroud[i];
        NSString *heightImage = heightBackground[i];
        NSString *title = titles[i];
        
        TabBarBtn *button = [TabBarBtn buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(kScreenWidth/titles.count*i, 0, kScreenWidth/titles.count, tabbarView.height);
        button.tag = i+100;
        [button setTitle:title forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:12];
        button.titleLabel.textAlignment = NSTextAlignmentCenter;
        [button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [button setTitleColor:kColor(46, 199, 255) forState:UIControlStateDisabled];
        [button setImage:[UIImage imageNamed:backImage] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:heightImage] forState:UIControlStateDisabled];
        [button addTarget:self action:@selector(selectedTab:) forControlEvents:UIControlEventTouchUpInside];
        [tabbarView addSubview:button];
        if (i == 0) {
            button.enabled = NO;
        }
    }
    
}


#pragma mark - -------------------------Actions-------------------
#pragma mark tabBar按钮点击事件
/**
 *  tabBar按钮点击事件
 *
 *  @param button tabBar按钮
 */
- (void)selectedTab:(UIButton *)button {
    
    self.selectedIndex = button.tag-100;
    
    
}
/**
 *  选择不同的tabBar按钮后修改不同的按钮状态
 *
 *  @param selectedIndex tabbarController的值
 */
- (void)setSelectedIndex:(NSUInteger)selectedIndex
{
    [super setSelectedIndex:selectedIndex];
    
    for (int i = 100; i<105; i++) {
        UIButton *btn =  (UIButton *)[tabbarView viewWithTag:i];
        btn.enabled = YES;
    }
    UIButton *btn =  (UIButton *)[tabbarView viewWithTag:selectedIndex+100];
    btn.enabled = NO;
    //每次点击后都需要将自己的tabbarview放置在最顶层，放置系统的tabbarItem覆盖
    [self.tabBar insertSubview:tabbarView atIndex:self.tabBar.subviews.count-1];
    
}

//系统自动调用默认方法隐藏掉tabbar上面的字体
+ (void)initialize
{
    // 通过appearance统一设置所有UITabBarItem的文字属性
    // 后面带有UI_APPEARANCE_SELECTOR的方法, 都可以通过appearance对象来统一设置
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = [UIFont systemFontOfSize:0];
    attrs[NSForegroundColorAttributeName] = [UIColor clearColor];
    
    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = [UIFont systemFontOfSize:0];
    attrs[NSForegroundColorAttributeName] = [UIColor clearColor];
    
    UITabBarItem *item = [UITabBarItem appearance];
    [item setTitleTextAttributes:attrs forState:UIControlStateNormal];
    [item setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

